stages:
  - test
  - build
  - deploy

code_linters:
  image: node:20-alpine
  stage: test
  before_script:
    - npm ci
  script:
    - npm run lint:code
    - npm run lint:style
  only:
    - merge_requests

build:
  image: node:20-alpine
  stage: build
  before_script:
    - npm ci
    - echo BASE_URL_API=$ENV_VARIABLE >> .env.production
  script:
    - npm run build
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME != "main"

deploy:
  image: node:20-alpine
  stage: deploy
  before_script:
    - npm ci
    - echo BASE_URL_API=$ENV_VARIABLE >> .env.production
    - apk update
    - apk add rsync openssh
    - mkdir ~/.ssh
    - chmod 600 ~/.ssh
    - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - echo "$KNOWN_HOST" >> ~/.ssh/known_hosts
    - chmod 600 ~/.ssh/known_hosts
  script:
    - npm run build
    - rsync -avz --delete dist/ andrey@147.45.106.246:/home/andrey/chill_build
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
